﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Mvc;

namespace PeaceBlog
{
    public static class ContentHelpers
    {
        public static MvcHtmlString CreateYoutubePlayer(string url)
        {
            string basePlayer = "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/{0}\" frameborder=\"0\" allowfullscreen></iframe>";
            return new MvcHtmlString(
                String.Format(
                    basePlayer,
                        Regex.Match(url,
                            @"youtu(?:\.be|be\.com)/(?:.*v(?:/|=)|(?:.*/)?)([a-zA-Z0-9-_]+)")
                            .Groups[0].Value.ToString()));
        }

        public static MvcHtmlString Title(this HtmlHelper helper, string title)
        {
            string baseTitle = "<div class=\"prl-article w-box prl-panel\"><div class=\"w-box-inner\"><h4 class=\"prl-category-title\">{0}</h4></div></div>";
            return new MvcHtmlString(string.Format(baseTitle, title));
        }

        public static MvcHtmlString ImageUrl(this HtmlHelper helper, int? ImageID, bool requied = false, int height = 0, int width = 0, string alt = "")
        {
            string baseImage = "<img src='{0}' height='{1}' width='{2}' alt='{3}' />";
            string notFoundImage = SiteHelpers.GetSetting("NotFoundImage");
            MvcHtmlString str = null;
            if (ImageID.HasValue)
            {
                using (PeaceBlogData.PeaceBlogEntity Context = new PeaceBlogData.PeaceBlogEntity())
                {
                    var file = Context.GetFile(ImageID.Value);
                    if (file != null)
                    {
                        if (height != 0 & width != 0)
                            str = new MvcHtmlString(String.Format(baseImage, file.FilePath, height, width, alt));
                        else
                            str = new MvcHtmlString(file.FilePath);
                    }
                }
            }
            if (str == null && requied)
                if (height != 0 & width != 0)
                    str = new MvcHtmlString(String.Format(baseImage, notFoundImage, height, width, alt));
                else
                    str = new MvcHtmlString(notFoundImage);
            return str;
        }

        public static MvcHtmlString GoogleAnalytics(this HtmlHelper htmlHelper, string izlemeKimligi)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<script>");
            sb.Append(Environment.NewLine);
            sb.Append("(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){");
            sb.Append(Environment.NewLine);
            sb.Append("(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),");
            sb.Append(Environment.NewLine);
            sb.Append("m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)");
            sb.Append(Environment.NewLine);
            sb.Append("})(window,document,'script','//www.google-analytics.com/analytics.js','ga');");
            sb.Append(Environment.NewLine);
            sb.Append("ga('create', '" + izlemeKimligi + "', 'auto');");
            sb.Append(Environment.NewLine);
            sb.Append("ga('send', 'pageview');");
            sb.Append(Environment.NewLine);
            sb.Append("</script>");
            return new MvcHtmlString(sb.ToString());
        }

        public static MvcHtmlString FacebookSDK(this HtmlHelper htmlHelper, string APIKey = "")
        {
            string _APIKey = (!string.IsNullOrEmpty(APIKey)) ? APIKey : SiteHelpers.GetSetting("FacebookAPIKey");
            string APIHtml = @"window.fbAsyncInit = function () {
            FB.init({
                appId: '" + _APIKey + @"',
                xfbml: true,
                version: 'v2.1'
            });
        };

        (function (d, s, id) {
            var js, fjs = d.getElementsByTagName(s)[0];
            if (d.getElementById(id)) { return; }
            js = d.createElement(s); js.id = id;
            js.src = ""//connect.facebook.net/en_US/sdk.js"";
            fjs.parentNode.insertBefore(js, fjs);
        }(document, 'script', 'facebook-jssdk'));";
            return new MvcHtmlString(APIHtml);
        }

        public static List<PeaceBlogData.Tables.Posts> GetPostFromAuthor(int AuthorID)
        {
            using (PeaceBlog.Controllers.API.PostsController API = new Controllers.API.PostsController())
            {
                return API.GetPostsFromAuthor(AuthorID);
            }
        }

        public static SelectList AuthorsList()
        {
            using (PeaceBlogData.PeaceBlogEntity Context = new PeaceBlogData.PeaceBlogEntity())
            {
                return new SelectList(Context.GetAllAuthors(), "AuthorID", "NameSurname");
            }
        }
    }
}